from django.db import models
from django.utils.safestring import mark_safe


# Create your models here.
class User(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True)
    name = models.CharField(verbose_name="用户名", max_length=32, default='')  #
    type = models.IntegerField(verbose_name="类型", default=1)  # 1普通用户|3教工号|4审核状态|5拒绝|6管理员|9禁用
    pswd = models.CharField(verbose_name="密码", max_length=32, default='')  # 管理员密码
    email = models.EmailField(verbose_name="邮箱", default='')
    phone = models.CharField(verbose_name="手机号", max_length=18, default='')
    time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    photo = models.ImageField(verbose_name="头像", upload_to='photo_touxiang/', default='')
    is_active = models.BooleanField('是否活跃', default=True)

    def __str__(self):
        return self.name

    def admin_sample(self):
        return mark_safe('<img src="/media/%s" height="90" width="90" />' % (self.photo,))

    admin_sample.short_description = 'Sample'
    admin_sample.allow_tags = True

    class Meta:
        # 数据库列表名
        db_table = 'User'
        # 后台管理名
        verbose_name_plural = '用户列表'


class Media(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True)
    title = models.CharField(verbose_name="题目", max_length=32, default='')  #
    user = models.ForeignKey(verbose_name="用户", to='User', on_delete=models.CASCADE)
    photo = models.ImageField(verbose_name="图片", upload_to='user/photo/', default='')
    synopsis = models.TextField(verbose_name="简介", default='')
    time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    vote = models.IntegerField(verbose_name="点赞数量", default=0)
    is_active = models.BooleanField('是否活跃', default=True)

    def __str__(self):
        return self.title

    def admin_sample(self):
        return mark_safe('<img src="/media/%s" height="90" width="90" />' % (self.photo,))

    admin_sample.short_description = 'Sample'
    admin_sample.allow_tags = True

    class Meta:
        # 数据库列表名
        db_table = 'Media'
        # 后台管理名
        verbose_name_plural = '投票管理'


class MyVote(models.Model):
    id = models.AutoField(verbose_name="编号", primary_key=True)
    media = models.ForeignKey(verbose_name="资源", to='Media', on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name="用户", to='User', on_delete=models.CASCADE)
    time = models.DateTimeField(verbose_name="投票时间", auto_now=True)
    is_active = models.BooleanField('是否活跃', default=True)

    def __str__(self):
        return self.time

    class Meta:
        # 数据库列表名
        db_table = 'Vote'
        # 后台管理名
        verbose_name_plural = '投票'


def __str__(self):
    return self.admin_sample
